<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>微信活动管理</title>
	<meta name="decorator" content="default"/>
    <style>
        .samll-input{
            width: 100px;
        }

    </style>
</head>
<body>
<div class="main-content">
    <tags:breadcrumb menuId="82ed44819420477db99ce3270363bf2c" />
    <div class="page-content">
        <form:form id="inputForm" modelAttribute="huodongEntity" action="" method="post" class="form-horizontal">
            <form:hidden path="id"/>
            <div class="form-group">
                <label class="col-sm-2 no-padding-right control-label">标题:</label>
                <div class="col-sm-4">
                    <form:input path="title" htmlEscape="false" maxlength="200" class="required"/>
                    <span class="important">*</span>
                </div>
                <label class="col-sm-2 no-padding-right control-label">抽奖次数:</label>
                <div class="col-sm-4">
                    <form:input path="count" htmlEscape="false" maxlength="20" class="required"/>
                    <span class="important">*</span>
                </div>
            </div><hr>
            <div class="form-group">
                <label class="col-sm-2 no-padding-right control-label">开始时间:</label>
                <div class="col-sm-4">
                    <input type="text" name="starttime" id="starttime" value='<fmt:formatDate value="${huodongEntity.starttime}" pattern="yyyy-MM-dd HH:mm:ss"/>' maxlength="20" class="required Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:true});"/>
                    <span class="important">*</span>
                </div>
                <label class="col-sm-2 no-padding-right control-label">结束时间:</label>
                <div class="col-sm-4">
                    <input type="text" name="endtime" id="endtime" value='<fmt:formatDate value="${huodongEntity.endtime}" pattern="yyyy-MM-dd HH:mm:ss"/>' maxlength="20" class="required Wdate" onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:true});"/>
                    <span class="important">*</span>
                </div>
            </div><hr>
            <div class="form-group">
                <label class="col-sm-2 no-padding-right control-label">公众号:</label>
                <div class="col-sm-4">
                    <form:select path="accountid" cssClass="col-md-6">
                        <form:option value="">--请选择--</form:option>
                        <form:options items="${accountList}" itemLabel="accountName" itemValue="id"/>
                    </form:select>
                    <span class="important">*</span>
                </div>
                <label class="col-sm-2 no-padding-right control-label">活动描述:</label>
                <div class="col-sm-4">
                    <form:input path="description" htmlEscape="false" maxlength="200" class="required col-sm-9"/>
                    <span class="important">*</span>
                </div>
            </div><hr>
            <div class="form-group">
                <table id="contentTable" class="table table-bordered" style="width: 96%;margin: 0 auto;">
                    <thead>
                        <tr>
                            <th>奖品名称</th>
                            <th>奖品总数</th>
                            <th>奖品价格</th>
                            <th>第一概率</th>
                            <th>第二概率</th>
                            <th>第三概率</th>
                            <th>状态</th>
                            <th>等级</th>
                            <th>排序</th>
                            <th><a href="javascript:;" onclick="addPrize();">添加</a></th>
                        </tr>
                    </thead>
                    <tbody>
                        <c:choose>
                            <c:when test="${not empty huodongEntity.id}">
                                <c:forEach items="${huodongEntity.prizeList}" var="prize" varStatus="statu">
                                    <tr>
                                        <td><input type="text" name="prizeList[${statu.index}].name" value="${prize.name}" class="samll-input"/></td>
                                        <td><input type="number" name="prizeList[${statu.index}].totalnum" value="${prize.totalnum}" min="0" class="samll-input"/></td>
                                        <td><input type="number" name="prizeList[${statu.index}].price" min="0" value="${prize.price}" class="samll-input"/></td>
                                        <td><input type="number" name="prizeList[${statu.index}].rate1" min="0" value="${prize.rate1}" class="samll-input"/></td>
                                        <td><input type="number" name="prizeList[${statu.index}].rate2" min="0" value="${prize.rate2}" class="samll-input"/></td>
                                        <td><input type="number" name="prizeList[${statu.index}].rate3" min="0" value="${prize.rate3}" class="samll-input"/></td>
                                        <td>
                                            <select name="prizeList[${statu.index}].status" id="status" class="samll-input">
                                                <option value="0">有效</option>
                                                <option value="1">无效</option>
                                            </select>
                                        </td>
                                        <td>
                                            <select name="prizeList[${statu.index}].level" id="level" class="samll-input">
                                                <option value="1">一等奖</option>
                                                <option value="2">二等奖</option>
                                                <option value="3">三等奖</option>
                                                <option value="4">四等奖</option>
                                                <option value="5">五等奖</option>
                                            </select>
                                        </td>
                                        <td><input type="text" name="prizeList[${statu.index}].sort" value="${prize.sort}" class="samll-input"/></td>
                                        <td style="vertical-align: middle;">
                                            <a href="javascript:;" name="123" onclick="deletePrize(this);">删除</a>
                                        </td>
                                    </tr>
                                </c:forEach>

                            </c:when>
                            <c:otherwise>
                                <tr>
                                    <td><input type="text" name="prizeList[0].name" class="samll-input"/></td>
                                    <td><input type="number" name="prizeList[0].totalnum" min="0" class="samll-input"/></td>
                                    <td><input type="number" name="prizeList[0].price" min="0" class="samll-input"/></td>
                                    <td><input type="number" name="prizeList[0].rate1" min="0" class="samll-input"/></td>
                                    <td><input type="number" name="prizeList[0].rate2" min="0" class="samll-input"/></td>
                                    <td><input type="number" name="prizeList[0].rate3" min="0" class="samll-input"/></td>
                                    <td>
                                        <select name="prizeList[0].status" id="status" class="samll-input">
                                            <option value="0">有效</option>
                                            <option value="1">无效</option>
                                        </select>
                                    </td>
                                    <td>
                                        <select name="prizeList[0].level" id="level" class="samll-input">
                                            <option value="1">一等奖</option>
                                            <option value="2">二等奖</option>
                                            <option value="3">三等奖</option>
                                            <option value="4">四等奖</option>
                                            <option value="5">五等奖</option>
                                        </select>
                                    </td>
                                    <td><input type="text" name="prizeList[0].sort" class="samll-input"/></td>
                                    <td style="vertical-align: middle;">
                                        <a href="javascript:;" name="123" onclick="deletePrize(this);">删除</a>
                                    </td>
                                </tr>
                            </c:otherwise>
                        </c:choose>

                    </tbody>
                </table>
            </div>
            <div class="form-actions">
                <div class="col-md-offset-4">
                    <shiro:hasPermission name="wechat:huodongEntity:edit"><input id="btnSubmit" class="btn btn-primary" type="button" value="保 存"/>&nbsp;</shiro:hasPermission>
                    <input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
                </div>
            </div>
        </form:form>
    </div>
</div>
    <script type="text/javascript">
        $(document).ready(function() {
            $("#name").focus();
            $("#btnSubmit").click(function(){
                if($("#inputForm").valid()){
                    $.post("${ctx}/wechat/huodongEntity/save",$("#inputForm").serialize(),function(data){
                        if(data.success){
                            showNote("success", function () {
                                window.location.href = "${ctx}/wechat/huodongEntity/list";
                            }, data.msg);
                        }else {
                            showNote("error", function () {
                                return false;
                            }, data.msg);
                        }
                    });
                }else {
                    showNote("error", function () {
                        return false;
                    }, "数据校验不正确,请输入正确的数据信息");
                }
            });
        });

        //添加活动奖项
        function addPrize(){
            var len = $("tbody").find("tr").length;
            var trstr = "<tr>";
            trstr += "<td><input type=\"text\" name=\"prizeList["+len+"].name\" class=\"samll-input\"/></td>";
            trstr += "<td><input type=\"number\" name=\"prizeList["+len+"].totalnum\" min=\"0\" class=\"samll-input\"/></td>";
            trstr += "<td><input type=\"number\" name=\"prizeList["+len+"].price\" min=\"0\" class=\"samll-input\"/></td>";
            trstr += "<td><input type=\"number\" name=\"prizeList["+len+"].rate1\" min=\"0\" class=\"samll-input\"/></td>"
            trstr += "<td><input type=\"number\" name=\"prizeList["+len+"].rate2\" min=\"0\" class=\"samll-input\"/></td>";
            trstr += "<td><input type=\"number\" name=\"prizeList["+len+"].rate3\" min=\"0\" class=\"samll-input\"/></td>";
            trstr += "<td>"+
                        "<select name=\"prizeList["+len+"].status\" class=\"samll-input\">"+
                        "<option value=\"0\">有效</option>"+
                        "<option value=\"1\">无效</option>"+
                        "</select>"+
                        "</td>";
            trstr += "<td>"+
                        "<select name=\"prizeList["+len+"].level\" class=\"samll-input\">"+
                        "<option value=\"1\">一等奖</option>"+
                        "<option value=\"2\">二等奖</option>"+
                        "<option value=\"3\">三等奖</option>"+
                        "<option value=\"4\">四等奖</option>"+
                        "<option value=\"5\">五等奖</option>"+
                        "</select>"+
                        "</td>";
            trstr += "<td><input type=\"text\" name=\"prizeList["+len+"].sort\" class=\"samll-input\"/></td>";
            trstr += "<td style=\"vertical-align: middle;\"><a href=\"javascript:;\" onclick=\"deletePrize(this);\">删除</a></td>";
            trstr += "</tr>";
            $("tbody").append(trstr);
        }

        //删除奖品
        function  deletePrize(obj){
            var len = $("tbody").find("tr").length;
            if(len > 1){
                $(obj).parent().parent().remove();
            }else {
                showNote("error", function () {
                    return false;
                }, "亲,至少要一个奖品哟");
            }
        }
    </script>
</body>
</html>
